package Call;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import Bfs.Bfs;
import Bfs.Car;

public class Call {
	ArrayList<Car> cars ;
	Bfs bfs;
	
	public Call(ArrayList<Car> cars, Bfs bfs)
	{
		this.cars = cars;
		this.bfs = bfs;
	}
	
	public Car call(String callPosition)
	{
		ArrayList<String> croute;
		ArrayList<String> route=null;
		Car scar=null;
		int size=10; //size should be longer than the longest path in the road map
		for(Car car : cars) 
		{
			croute = car.stringToRoute(car.getCurrentRoute());
			if(car.getPassengerNo()<=3 && !car.isOnCall()) {
				//first checks if the call's source is already in the car's currentRoute 	
				if(croute.contains(callPosition))	{
				
					route = bfs.bfs(car.getPosition(),callPosition);
				 	if(route.size()<size) 
				 	{
				 		scar=car;
				 		size=route.size();
				 		scar.setCallRoute(scar.routeToString(route));
				 		scar.setCurrentRoute(scar.getCurrentRoute());
				 	}
				}
				
				//if the call's source is not in the currentRoute's route
				else if(!croute.contains(callPosition))
				{
					
					route = bfs.bfs(car.getPosition(),callPosition);
					
				 	if(route.size()<size) 
				 	{
				 		scar=car;
				 		size=route.size();
			 			scar.setCallRoute(scar.routeToString(route));
				 		scar.setCurrentRoute(scar.getCurrentRoute()+","+scar.getCallRoute());
			
				 	}
				}
			}		
		}
				
		return scar;
		
		
	}
	
	
}
